package com.noovo.comport.server;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class ComSQLiteOpenHelper extends SQLiteOpenHelper {
	private static final String DATABASE_NAME = "Com.db";
	private static final int DATABASE_VERSION = 1;
	private static final String DB_TABLE_COM_SET = "ComSet";
	private static final String DB_TABLE_COM_RECEIVE = "ComReceive";

	public ComSQLiteOpenHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
        db.execSQL("DROP TABLE IF EXISTS ComTable");
        String DB_CREATE_SET = "create table if not exists " + DB_TABLE_COM_SET
    			+ " (id integer primary key autoincrement, " + ComInfo.DB_ITEM_PORT + " integer, " + ComInfo.DB_ITEM_NAME
    			+ " text, " + ComInfo.DB_ITEM_RATE + " integer, " + ComInfo.DB_ITEM_DATABITS + " integer, "
    			+ ComInfo.DB_ITEM_STOPBITS + " integer, " + ComInfo.DB_ITEM_PARITY + " text, " + ComInfo.DB_ITEM_RUN
    			+ " text, " + ComInfo.DB_ITEM_LOOP + " text, " + ComInfo.DB_ITEM_DELAY + " integer, " + ComInfo.DB_ITEM_MSG
    			+ " BLOB, " + ComInfo.DB_ITEM_SEND + " text);";
        db.execSQL(DB_CREATE_SET);
        String DB_CREATE_RECEIVE = "create table if not exists " + DB_TABLE_COM_RECEIVE + " (id integer primary key autoincrement, "+ ComInfo.DB_ITEM_PORT + " integer, " + ComInfo.DB_ITEM_NAME + " text";
        for (int i = 0; i < ComInfo.DB_ITEM_RECEIVE_MAX; i++) {
        	DB_CREATE_RECEIVE = DB_CREATE_RECEIVE + ", " + ComInfo.DB_ITEM_RECEIVE + "_" + i + " BLOB";
		}
        DB_CREATE_RECEIVE = DB_CREATE_RECEIVE + ");";
        db.execSQL(DB_CREATE_RECEIVE);
	}

	public Cursor querySet(int port, String[] projection) {
		String selection = ComInfo.DB_ITEM_PORT + "=?";
		String[] selectionArgs = { String.valueOf(port) };
		Cursor cursor = getReadableDatabase().query(DB_TABLE_COM_SET, projection, selection, selectionArgs, null, null, null);
		return cursor;
	}
	
	public Cursor queryReceive(int port, String[] projection) {
		String selection = ComInfo.DB_ITEM_PORT + "=?";
		String[] selectionArgs = { String.valueOf(port) };
		Cursor cursor = getReadableDatabase().query(DB_TABLE_COM_RECEIVE, projection, selection, selectionArgs, null, null, null);
		return cursor;
	}

	public void updateSet(int port, ContentValues values) {
		SQLiteDatabase db = getWritableDatabase();
		String selection = ComInfo.DB_ITEM_PORT + "=?";
		String[] selectionArgs = { String.valueOf(port) };
		Cursor cursor = db.query(DB_TABLE_COM_SET, null, selection, selectionArgs, null, null, null);
		if (cursor != null && cursor.getCount() == 1) {
			int id = db.update(DB_TABLE_COM_SET, values, selection, selectionArgs);
			cursor.close();
		} else {
			long id = db.insert(DB_TABLE_COM_SET, null, values);
		}
	}
	
	public void updateReceive(int port, ContentValues values) {
		SQLiteDatabase db = getWritableDatabase();
		String selection = ComInfo.DB_ITEM_PORT + "=?";
		String[] selectionArgs = { String.valueOf(port) };
		Cursor cursor = db.query(DB_TABLE_COM_RECEIVE, null, selection, selectionArgs, null, null, null);
		if (cursor != null && cursor.getCount() == 1) {
			int id = db.update(DB_TABLE_COM_RECEIVE, values, selection, selectionArgs);
			cursor.close();
		} else {
			long id = db.insert(DB_TABLE_COM_RECEIVE, null, values);
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	}
}
